package in.iqing.control.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.model.bean.DownloadedBook;

/**
 * Created by ZuoShu on 2015/7/17.
 */
public class DownloadBookAdapter extends RecyclerView.Adapter<DownloadBookAdapter.ViewHolder> {
    private Context context;

    private List<DownloadedBook> books;
    private Transformation coverTransformation;

    private boolean isOperating;
    private Set<DownloadedBook> selectedBook;
    private Callback listener;

    public interface Callback {
        public void onItemClick(DownloadedBook book);

        public void onCheckedChange(DownloadedBook book, boolean checked);
    }

    public DownloadBookAdapter(Context context) {
        this.context = context;
        coverTransformation = new RoundedTransformationBuilder()
                .cornerRadius(context.getResources().getDimension(R.dimen.round_image_radius))
                .borderWidth(context.getResources().getDimension(R.dimen.image_border_width))
                .borderColor(context.getResources().getColor(R.color.iqing_yellow))
                .oval(false)
                .build();
        selectedBook = new HashSet<>();
    }

    public void setListener(Callback listener) {
        this.listener = listener;
    }

    public boolean isOperating() {
        return isOperating;
    }

    public void setIsOperating(boolean isOperating) {
        this.isOperating = isOperating;
    }

    public void selectAll() {
        selectedBook.addAll(books);
    }

    public void deselectAll() {
        selectedBook.removeAll(books);
    }

    public Set<DownloadedBook> getSelectedBook() {
        return selectedBook;
    }

    public List<DownloadedBook> getBooks() {
        return books;
    }

    public void setBooks(List<DownloadedBook> books) {
        this.books = books;
    }

    public void deleteSelected() {
        books.removeAll(selectedBook);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_downloaded_book, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DownloadedBook item = books.get(position);
        holder.book = item;
        holder.titleText.setText(item.getTitle());
        holder.authorText.setText(item.getAuthor());
        holder.pressText.setText(item.getPress());
        holder.readText.setText(item.getReadPercent());
        holder.downloadText.setText(item.getDownloadPercent());
        RequestCreator createor = TextUtils.isEmpty(item.getCover())
                ? Picasso.with(context).load(R.drawable.image_loading_1x1)
                : Picasso.with(context).load(new File(item.getCover()));
        createor.placeholder(R.drawable.image_loading_1x1)
                .error(R.drawable.image_error)
                .transform(coverTransformation)
                .resizeDimen(R.dimen.download_finish_book_cover_width, R.dimen.download_finish_book_cover_height)
                .centerCrop()
                .into(holder.coverImage);
        holder.selected.setVisibility(isOperating ? View.VISIBLE : View.GONE);
        holder.selected.setChecked(selectedBook.contains(holder.book));
    }

    @Override
    public int getItemCount() {
        return books == null ? 0 : books.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.cover_image)
        ImageView coverImage;

        @Bind(R.id.title_text)
        TextView titleText;

        @Bind(R.id.author_text)
        TextView authorText;

        @Bind(R.id.press_text)
        TextView pressText;

        @Bind(R.id.download_progress)
        TextView downloadText;

        @Bind(R.id.read_progress)
        TextView readText;

        @Bind(R.id.option_checkbox)
        CheckBox selected;

        DownloadedBook book;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.book_item_layout)
        public void onItemClick(View v) {
            if (isOperating) {
                selected.setChecked(!selectedBook.contains(book));
            } else {
                if (listener != null) {
                    listener.onItemClick(book);
                }
            }
        }

        @OnCheckedChanged(R.id.option_checkbox)
        public void onSelectedCheckChange(CompoundButton compoundButton, boolean checked) {
            if (checked) {
                selectedBook.add(book);
            } else {
                selectedBook.remove(book);
            }
            if (listener != null) {
                listener.onCheckedChange(book, checked);
            }
        }
    }
}
